using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;

using OpenSocial.Net.Atom;

namespace OpenSocial.Net.People
{
	public class PeopleBuilder : AtomBuilder
	{
		//<entry xmlns='http://www.w3.org/2005/Atom' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005'>
		//  <id>http://sandbox.orkut.com:80/feeds/people/14358878523263729569</id>
		//  <updated>2007-10-28T14:01:29.948-07:00</updated>
		//  <title>Elizabeth Bennet</title>
		//  <link rel='thumbnail' type='image/*' href='http://img1.orkut.com/images/small/1193601584/115566312.jpg'/>
		//  <link rel='alternate' type='text/html' href='http://orkut.com/Profile.aspx?uid=17583631990196664929'/>
		//  <link rel='self' type='application/atom+xml' href='http://sandbox.orkut.com:80/feeds/people/14358878523263729569'/>
		//  <georss:where>
		//    <gml:Point xmlns:gml='http://www.opengis.net/gml'>
		//      <gml:pos>51.668674 -0.066235</gml:pos>
		//    </gml:Point>
		//  </georss:where>
		//  <gd:extendedProperty name='lang' value='en-US'/>
		//  <gd:postalAddress/>
		//</entry>

		public PeopleBuilder()
		{

		}

		/// <summary>
		/// Builds a FriendList object from the provided ATOM string.
		/// </summary>
		/// <param name="atom">The atom.</param>
		/// <returns></returns>
		public FriendList BuildFriendList(string atom)
		{
			FriendList flist = new FriendList();
			base.BuildAtomFeedBase(atom, flist);
		
			//get friends from feed
			XPathNodeIterator iter = base.GetNodes(atom, "/atom:feed/atom:entry");
			while (iter.MoveNext())
			{
				Friend friend = new Friend();
				base.BuildAtomEntry(iter.Current.OuterXml, friend);

				//Add custom friend parsing here
				friend.UserID = ulong.Parse(friend.ID.Substring(friend.ID.LastIndexOf('/') + 1));

				flist.Friends.Add(friend);
			}

			return flist;
		}

		/// <summary>
		/// Builds a Person object from the provided ATOM string.
		/// </summary>
		/// <param name="atom">The atom.</param>
		/// <returns></returns>
		public Person BuildPerson(string atom)
		{
			Person person = new Person();
			base.BuildAtomEntry(atom, person);

			//TODO: Add custom person parsing here.

			return person;
		}
	}
}
